package com.xinxin.leetcode.problem557;

/**
 * @author ShiXinXin
 * @date 2020-01-17 11:59
 */
class Solution {
    private void swap(char[] chs, int begin, int end) {
        while (begin < end) {
            char temp = chs[begin];
            chs[begin++] = chs[end];
            chs[end--] = temp;
        }
    }

    public String reverseWords(String s) {
        char[] chs = s.toCharArray();
        int begin = 0;
        for (int i = 0; i < chs.length; i++) {
            if (i == chs.length - 1) {
                swap(chs, begin, i);
            } else if (chs[i] == ' ') {
                swap(chs, begin, i - 1);
                begin = i + 1;
            }
        }
        return new String(chs);
    }

    public static void main(String[] args) {
        System.out.println(new Solution().reverseWords("Let's take LeetCode contest"));
    }
}
